﻿using IntoStationSystem.Db;
using IntoStationSystem.Entity;
using IntoStationSystem.Global;
using IntoStationSystem.PLC;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;

namespace IntoStationSystem.ViewModels
{
    [Serializable]
    public class ManualViewModel : BindableBase
    {
        #region field
        private int pageIndex = 1;
        private List<Cylinder> cylinders = new List<Cylinder>();
        bool[] datas = new bool[2];

        public int ReadStatus = -1;
        Service service => Locator.Instance.Service;
        #endregion
        #region property
        private string left1 = "去工作中";
        private string left2 = "到达工位";
        private string right1 = "去原点中";
        private string right2 = "到达原点";


        private string left = "去工位中\n到达工位";
        private string right = "去原点中\n到达原点";
        private string center1 = "备用";
        private string center2 = "备用";
        private string center3 = "备用";
        private string center4 = "备用";
        private string center5 = "备用";

        private string curPage = "第1页";

        private bool forward1;
        private bool forwardArrived1;
        private bool forwardSafety1;
        private bool backward1;
        private bool backwardArrived1;
        private bool backwardSafety1;
        private bool forward2;
        private bool forwardArrived2;
        private bool forwardSafety2;
        private bool backward2;
        private bool backwardArrived2;
        private bool backwardSafety2;
        private bool forward3;
        private bool forwardArrived3;
        private bool forwardSafety3;
        private bool backward3;
        private bool backwardArrived3;
        private bool backwardSafety3;
        private bool forward4;
        private bool forwardArrived4;
        private bool forwardSafety4;
        private bool backward4;
        private bool backwardArrived4;
        private bool backwardSafety4;
        private bool forward5;
        private bool forwardArrived5;
        private bool forwardSafety5;
        private bool backward5;
        private bool backwardArrived5;
        private bool backwardSafety5;

        public string Left1
        {
            get => left1; set => SetProperty(ref left1, value);
        }
        public string Left2
        {
            get => left2; set => SetProperty(ref left2, value);
        }
        public string Right1
        {
            get => right1; set => SetProperty(ref right1, value);
        }
        public string Right2
        {
            get => right2; set => SetProperty(ref right2, value);
        }
        public string Left
        {
            get => left;
            set => SetProperty(ref left, value);
        }
        public string Right
        {
            get => right;
            set => SetProperty(ref right, value);
        }
        public string Center1
        {
            get => center1;
            set => SetProperty(ref center1, value);
        }
        public string Center2
        {
            get => center2;
            set => SetProperty(ref center2, value);
        }
        public string Center3
        {
            get => center3;
            set => SetProperty(ref center3, value);
        }
        public string Center4
        {
            get => center4;
            set => SetProperty(ref center4, value);
        }
        public string Center5
        {
            get => center5;
            set => SetProperty(ref center5, value);
        }
        public string CurPage
        {
            get => curPage;
            set => SetProperty(ref curPage, value);
        }

        public bool Forward1
        {
            get => forward1; set => SetProperty(ref forward1, value);
        }
        public bool Forward2
        {
            get => forward2; set => SetProperty(ref forward2, value);
        }
        public bool Forward3
        {
            get => forward3; set => SetProperty(ref forward3, value);
        }
        public bool Forward4
        {
            get => forward4; set => SetProperty(ref forward4, value);
        }
        public bool Forward5
        {
            get => forward5; set => SetProperty(ref forward5, value);
        }
        public bool Backward1
        {
            get => backward1; set => SetProperty(ref backward1, value);
        }
        public bool Backward2
        {
            get => backward2; set => SetProperty(ref backward2, value);
        }
        public bool Backward3
        {
            get => backward3; set => SetProperty(ref backward3, value);
        }
        public bool Backward4
        {
            get => backward4; set => SetProperty(ref backward4, value);
        }
        public bool Backward5
        {
            get => backward5; set => SetProperty(ref backward5, value);
        }
        public bool ForwardArrived1
        {
            get => forwardArrived1; set => SetProperty(ref forwardArrived1, value);
        }
        public bool ForwardArrived2
        {
            get => forwardArrived2; set => SetProperty(ref forwardArrived2, value);
        }
        public bool ForwardArrived3
        {
            get => forwardArrived3; set => SetProperty(ref forwardArrived3, value);
        }
        public bool ForwardArrived4
        {
            get => forwardArrived4; set => SetProperty(ref forwardArrived4, value);
        }
        public bool ForwardArrived5
        {
            get => forwardArrived5; set => SetProperty(ref forwardArrived5, value);
        }
        public bool BackwardArrived1
        {
            get => backwardArrived1; set => SetProperty(ref backwardArrived1, value);
        }
        public bool BackwardArrived2
        {
            get => backwardArrived2; set => SetProperty(ref backwardArrived2, value);
        }
        public bool BackwardArrived3
        {
            get => backwardArrived3; set => SetProperty(ref backwardArrived3, value);
        }
        public bool BackwardArrived4
        {
            get => backwardArrived4; set => SetProperty(ref backwardArrived4, value);
        }
        public bool BackwardArrived5
        {
            get => backwardArrived5; set => SetProperty(ref backwardArrived5, value);
        }
        public bool ForwardSafety1
        {
            get => forwardSafety1; set => SetProperty(ref forwardSafety1, value);
        }
        public bool ForwardSafety2
        {
            get => forwardSafety2; set => SetProperty(ref forwardSafety2, value);
        }
        public bool ForwardSafety3
        {
            get => forwardSafety3; set => SetProperty(ref forwardSafety3, value);
        }
        public bool ForwardSafety4
        {
            get => forwardSafety4; set => SetProperty(ref forwardSafety4, value);
        }
        public bool ForwardSafety5
        {
            get => forwardSafety5; set => SetProperty(ref forwardSafety5, value);
        }

        public bool BackwardSafety1
        {
            get => backwardSafety1; set => SetProperty(ref backwardSafety1, value);
        }
        public bool BackwardSafety2
        {
            get => backwardSafety2; set => SetProperty(ref backwardSafety2, value);
        }
        public bool BackwardSafety3
        {
            get => backwardSafety3; set => SetProperty(ref backwardSafety3, value);
        }
        public bool BackwardSafety4
        {
            get => backwardSafety4; set => SetProperty(ref backwardSafety4, value);
        }
        public bool BackwardSafety5
        {
            get => backwardSafety5; set => SetProperty(ref backwardSafety5, value);
        }
        #endregion
        #region method

        public ManualViewModel()
        {
            //DbHelper.Log("ManualViewModel");
            Config();
            RefreshPage(pageIndex);
        }

        private void Config()
        {
            //当检测到没有配置时，在数据库中写入默认值
            cylinders = DbHelper.GetConfig<Cylinder>();
            if (cylinders == null || cylinders.Count == 0)
            {
                for (int i = 0; i < Fields.CylinderCount; i++)
                {
                    cylinders.Add(new Cylinder()
                    {
                        Name = $"备用",
                    });
                }
                DbHelper.SetDefault<Cylinder>();
            }
        }

        public void Set(int index)
        {
            var pos = Fields.Map["Manual"];
            int carry = index / 10;
            int ret = index % 10;
            int offset = pos.First + carry + (pageIndex - 1) * Fields.CylinderCount;
            service.WriteBool(offset, ret, true);
        }

        public void Reset(int index)
        {
            var pos = Fields.Map["Manual"];
            int carry = index / 10;
            int ret = index % 10;
            int offset = pos.First + carry + (pageIndex - 1) * Fields.CylinderCount;
            service.WriteBool(offset, ret, false);
        }

        private void GetReadStatus(int pageIndex)
        {
            int count = cylinders.Count;
            int ret = count % Fields.CylinderCount;
            int carry = count / Fields.CylinderCount;
            if (pageIndex >= carry + 1)
            {
                ReadStatus = count > 0 ? ret : -1;
            }
            else
            {
                ReadStatus = 0;
            }
        }

        private void PageTurnning(object obj)
        {
            int index = Convert.ToInt32(obj);
            int ret = cylinders.Count % Fields.CylinderCount;
            int maxPageIndex = -1;
            if (ret == 0)
            {
                maxPageIndex = cylinders.Count / Fields.CylinderCount;
            }
            else
            {
                maxPageIndex = cylinders.Count / Fields.CylinderCount + 1;
            }
            if (pageIndex >= maxPageIndex && index == 1) return;

            if (index == 0 && pageIndex > 1)
            {
                pageIndex--;
            }
            if (index == 1)
            {
                pageIndex++;
            }
            CurPage = $"第 {pageIndex} 页";
            service.SetPageID((short)(10 + pageIndex));
            RefreshPage(pageIndex);
            GetReadStatus(pageIndex);
        }

        private void RefreshPage(int pageIndex)
        {
            int pos = (pageIndex - 1) * Fields.CylinderCount;
            int count = cylinders.Count;
            Center1 = (0 + pos) < count ? cylinders[0 + pos].Name : "备用";
            Center2 = (1 + pos) < count ? cylinders[1 + pos].Name : "备用";
            Center3 = (2 + pos) < count ? cylinders[2 + pos].Name : "备用";
            Center4 = (3 + pos) < count ? cylinders[3 + pos].Name : "备用";
            Center5 = (4 + pos) < count ? cylinders[4 + pos].Name : "备用";
        }
        #endregion
        #region command
        private DelegateCommand<object> pageTurnCmd;
        public DelegateCommand<object> PageTurnCmd => pageTurnCmd ?? (pageTurnCmd = new DelegateCommand<object>(PageTurnning));

        #endregion
    }
}
